<?php

/**
 * @author HungPV <phamvanhung0818@gmail.com>
 * @todo managment project categories controller
 */
class pcategories extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('category');
    }

    function index() {
        $this->user->check_permission();
        if ($this->input->post('name')) {
            $name = $this->input->post('name');
            $description = $this->input->post('description');
            $permalink = $this->category->generate_permalink($name);

            if ($this->category->add($name, $description,'project',$permalink)) {
                $this->session->set_flashdata('insert_pcat_success', true);
            } else {
                $this->session->set_flashdata('insert_pcat_error', true);
            }
            redirect('administrator/pcategories');
        }

        // get list categories
        $categories = $this->category->get(-1,-1,0,'project');
        $data['categories'] = $categories;

        $data['view'] = 'pcategories_index';
        $this->load->view('template', $data);
    }

    function edit($id = 0) {
        $this->user->check_permission();
        if ($this->input->post('name')) {
            $id = $this->input->post('id');
            $name = $this->input->post('name');
            $description = $this->input->post('description');

            if ($this->category->edit($id, $name, $description)) {
                $this->session->set_flashdata('update_pcat_success', true);
            } else {
                $this->session->set_flashdata('update_pcat_error', true);
            }
            redirect('administrator/pcategories/edit/' . $id);
        }

        // get current category
        $category = $this->category->get($id);
        if ($category != NULL) {
            $data['category'] = $category;
        }

        // get list categories
        $categories = $this->category->get(-1,-1,0,'project');
        $data['categories'] = $categories;

        $data['view'] = 'pcategories_update';
        $this->load->view('template', $data);
    }

    function delete() {
        $this->user->check_permission();
        $id = $this->input->post('id');
        if ($this->category->delete($id)) {
            echo json_encode(array('result' => TRUE));
        } else {
            echo json_encode(array('result' => FALSE));
        }
    }

}

?>
